<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="本地化" tabindex="-1"><a class="header-anchor" href="#本地化"><span>本地化</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#publishing-the-language-files">发布语言文件</a></li>
<li><a href="#configuring-the-locale">配置区域</a></li>
<li><a href="#pluralization-language">多语言</a></li>
</ul>
</li>
<li><a href="#defining-translation-strings">定义翻译字符串</a>
<ul>
<li><a href="#using-short-keys">使用短键</a></li>
<li><a href="#using-translation-strings-as-keys">使用翻译字符串作为键</a></li>
</ul>
</li>
<li><a href="#retrieving-translation-strings">检索翻译字符串</a>
<ul>
<li><a href="#replacing-parameters-in-translation-strings">替换翻译字符串中的参数</a></li>
<li><a href="#pluralization">复数化</a></li>
</ul>
</li>
<li><a href="#overriding-package-language-files">重写扩展包的语言文件</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<blockquote>
<p>[!NOTE ]<br>
默认情况下，Laravel 应用程序框架不包括 lang 目录。如果您想自定义 Laravel 的语言文件，可以通过Artisan 命令 <code v-pre>lang:publish</code> 发布它们。</p>
</blockquote>
<p>Laravel的本地化功能提供了一种便捷的方式来检索各种语言的字符串，这使您能够轻松地在应用程序中实现多语言的支持。</p>
<p>Laravel 提供两种管理翻译字符串的方式。第一种，语言字符串可以存储在应用程序的 lang 目录中的文件里。在这个目录中，可能会为应用程序支持的每种语言创建子目录。这是 Laravel 用于管理内置功能（如验证错误消息）的翻译字符串的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    <span class="token operator">/</span>en</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line">    <span class="token operator">/</span>es</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第二种，翻译字符串可以定义在放置在 lang 目录中的 JSON 文件中。采用这种方法时，您的应用程序支持的每种语言都会在这个目录中有一个对应的 JSON 文件。这种方法适用于具有大量翻译字符串需求的应用程序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    en<span class="token operator">.</span>json</span>
<span class="line">    es<span class="token operator">.</span>json</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>我们将在本文档中讨论管理翻译字符串的每种方法。</p>
<h3 id="发布语言文件" tabindex="-1"><a class="header-anchor" href="#发布语言文件"><span>发布语言文件</span></a></h3>
<p>默认情况下，Laravel 应用程序框架不包括 lang 目录。如果您想要自定义 Laravel 的语言文件或者创建自己的语言文件，您应通过Artisan 命令 <code v-pre>lang:publish</code> 来生成 lang 目录。<code v-pre>lang:publish</code> 命令将在您的应用程序中创建 lang 目录，并发布 Laravel 使用的默认语言文件集合：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan lang:publish</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置区域" tabindex="-1"><a class="header-anchor" href="#配置区域"><span>配置区域</span></a></h3>
<p>应用程序的默认语言存储在 <code v-pre>config/app.php</code> 配置文件的 <code v-pre>locale</code> 配置选项中，该选项通常使用 <code v-pre>APP_LOCALE</code> 环境变量进行设置。您可以根据应用需要自由修改此值。</p>
<p>您还可以配置「备用语言」，当默认语言不包含给定的翻译字符串时将使用该语言。与默认语言一样，备用语言也在 <code v-pre>config/app.php</code> 配置文件中进行配置，其值通常使用 <code v-pre>APP_FALLBACK_LOCALE</code> 环境变量进行设置。</p>
<p>您可以在运行时使用 <code v-pre>App</code> 门面（Facade）提供的 <code v-pre>setLocale</code> 方法修改单个 HTTP 请求的默认语言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/greeting/{locale}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$locale</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token function">in_array</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'es'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'fr'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">setLocale</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确定当前语言环境" tabindex="-1"><a class="header-anchor" href="#确定当前语言环境"><span>确定当前语言环境</span></a></h4>
<p>您可以使用 <code v-pre>App</code> 门面（Facade）上的 <code v-pre>currentLocale</code> 和 <code v-pre>isLocale</code> 方法来确定当前语言环境或检查语言环境是否为指定的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$locale</span> <span class="token operator">=</span> <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">currentLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">isLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="多语言" tabindex="-1"><a class="header-anchor" href="#多语言"><span>多语言</span></a></h3>
<p>您可以指示 Laravel 的 「复数化器（pluralizer）」（由 Eloquent 和框架的其他部分使用，将单数字符串转换为复数字符串）使用英语以外的语言。这可以通过在应用程序的某个服务提供者的 <code v-pre>boot</code> 方法中调用 <code v-pre>useLanguage</code> 方法来实现。复数化器当前支持的语言有：<code v-pre>french（法语）</code> 、<code v-pre>norwegian-bokmal（挪威语-博克马尔语）</code> 、 <code v-pre>portuguese（葡萄牙语）</code> 、 <code v-pre>spanish（西班牙语）</code> 和 <code v-pre>turkish（土耳其语）</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Pluralizer</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导所有应用服务.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Pluralizer</span><span class="token operator">::</span><span class="token function">useLanguage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'spanish'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     </span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...     </span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
如果您自定义复数化器的语言，则应显式定义 Eloquent 模型的 <a href="https://learnku.com/docs/laravel/11.x/eloquent#table-names" target="_blank" rel="noopener noreferrer">表名</a>.</p>
</blockquote>
<h2 id="定义翻译字符串" tabindex="-1"><a class="header-anchor" href="#定义翻译字符串"><span>定义翻译字符串</span></a></h2>
<h3 id="使用短键" tabindex="-1"><a class="header-anchor" href="#使用短键"><span>使用短键</span></a></h3>
<p>通常情况下，翻译字符串存储在 <code v-pre>lang</code> 目录下的文件中。在这个目录中，应该为应用程序支持的每种语言创建一个子目录。这是 Laravel 用于管理内置功能（如验证错误消息）的翻译字符串的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>lang</span>
<span class="line">    <span class="token operator">/</span>en</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line">    <span class="token operator">/</span>es</span>
<span class="line">        messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有语言文件返回一个键值对字符串数组。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// lang/en/messages.php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application!'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
对于按地区不同的语言，应根据 ISO 15897 的命名规范命名语言目录。例如，应该使用 「en_GB」 表示英国英语，而不是 「en-gb」。</p>
</blockquote>
<h3 id="使用翻译字符串作为键" tabindex="-1"><a class="header-anchor" href="#使用翻译字符串作为键"><span>使用翻译字符串作为键</span></a></h3>
<p>对于有大量需翻译字符串的应用，为每个字符串定义一个「短键」并在视图中引用这些键时，可能会变得非常混乱，而且不断为应用支持的每个翻译字符串增加新的键会日益繁琐。</p>
<p>因此，Laravel 还支持使用字符串的「默认（default）」翻译作为键来定义翻译字符串。我们可以将使用翻译字符串作为键的语言文件存储为 JSON 文件，并存放在 <code v-pre>lang</code> 目录中，以此实现这样的功能。例如，如果您的应用有一个西班牙语翻译，您应该创建一个 <code v-pre>lang/es.json</code> 文件：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"I love programming."</span><span class="token operator">:</span> <span class="token string">"Me encanta programar."</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="键-文件-冲突问题" tabindex="-1"><a class="header-anchor" href="#键-文件-冲突问题"><span>键 / 文件 冲突问题</span></a></h4>
<p>您不应该定义与其他翻译文件名冲突的翻译字符串键。例如，在存在 <code v-pre>nl/action.php</code> 文件但不存在 <code v-pre>nl.json</code> 文件的情况下，为「NL」语言环境翻译 <code v-pre>__('Action')</code> ，这将导致翻译器返回 <code v-pre>nl/action.php</code> 文件的整个内容。</p>
<h2 id="检索翻译字符串" tabindex="-1"><a class="header-anchor" href="#检索翻译字符串"><span>检索翻译字符串</span></a></h2>
<p>您可以使用 <code v-pre>__</code> 辅助函数从语言文件中检索翻译字符串。如果您使用「短键」来定义翻译字符串，则应该使用“点”语法将包含键的文件和键本身传递给 <code v-pre>__</code> 函数。例如，从 <code v-pre>lang/en/messages.php</code> 语言文件中检索 <code v-pre>welcome</code> 翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译字符串不存在，<code v-pre>__</code> 函数将返回翻译字符串的键。因此，使用上面的示例，如果翻译字符串不存在， <code v-pre>__</code> 函数将返回 <code v-pre>messages.welcome</code>。</p>
<p>如果您将 <a href="#using-translation-strings-as-keys">默认翻译字符串用作翻译键</a> ，您应将默认的翻译字符串传递给 <code v-pre>__</code> 函数；</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'I love programming.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>再次强调，如果翻译字符串不存在，<code v-pre>__</code> 函数将返回它所接收到的翻译字符串键。</p>
<p>如果您正在使用 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade模板引擎</a> ，您可以使用 <code v-pre>{{ }}</code> 输出语法来显示翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="在翻译字符串中替换参数" tabindex="-1"><a class="header-anchor" href="#在翻译字符串中替换参数"><span>在翻译字符串中替换参数</span></a></h3>
<p>您可以根据需要在翻译字符串中定义占位符。所有的占位符都以 <code v-pre>:</code> 开头。例如，您可以定义一个带有占位符名称的欢迎消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :name'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要在检索翻译字符串时替换占位符，可以将替换值的数组作为第二个参数传递给 <code v-pre>__</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您的占位符全部是大写字母，或者只有第一个字母是大写的，翻译后的值将会相应地保持首字母大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :NAME'</span><span class="token punctuation">,</span> <span class="token comment">// Welcome, DAYLE</span></span>
<span class="line"><span class="token string single-quoted-string">'goodbye'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Goodbye, :Name'</span><span class="token punctuation">,</span> <span class="token comment">// Goodbye, Dayle</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="对象替换格式" tabindex="-1"><a class="header-anchor" href="#对象替换格式"><span>对象替换格式</span></a></h4>
<p>如果尝试将对象作为翻译占位符，将会调用对象的 <code v-pre>__toString</code> 方法。<a href="https://www.php.net/manual/en/language.oop5.magic.php#object.tostring" target="_blank" rel="noopener noreferrer"><code v-pre>__toString</code></a> 方法是 PHP 内置的「魔术方法」之一。然而，有时您可能无法控制给定类的 <code v-pre>__toString</code> 方法，特别是当您与属于第三方库的类进行交互时。</p>
<p>在这些情况下，Laravel 允许您为特定类型的对象注册自定义格式化处理程序。要实现这一点，您可以调用翻译器的 <code v-pre>stringable</code> 方法。<code v-pre>stringable</code> 方法接受一个闭包，该闭包应该对其所负责格式化的对象类型进行类型提示。通常，<code v-pre>stringable</code> 方法在您应用的 <code v-pre>AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Lang</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Money<span class="token punctuation">\</span>Money</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导所有应用服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Lang</span><span class="token operator">::</span><span class="token function">stringable</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Money</span> <span class="token variable">$money</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$money</span><span class="token operator">-></span><span class="token function">formatTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en_GB'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复数化" tabindex="-1"><a class="header-anchor" href="#复数化"><span>复数化</span></a></h3>
<p>复数化是一个复杂的问题，因为不同的语言具有各种复杂的复数化规则；然而，Laravel 可以帮助您根据您定义的复数化规则以不同的方式翻译字符串。使用 <code v-pre>|</code> 字符，您可以区分字符串的单数和复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'There is one apple|There are many apples'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，在使用 <a href="#using-translation-strings-as-keys">翻译字符串作为键</a> 时，也支持复数化：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"There is one apple|There are many apples"</span><span class="token operator">:</span> <span class="token string">"Hay una manzana|Hay muchas manzanas"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您甚至可以创建更复杂的复数化规则，指定多个值范围的翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|[1,19] There are some|[20,*] There are many'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在定义具有复数化选项的翻译字符串后，您可以使用 <code v-pre>trans_choice</code> 函数根据给定的「数量（count）」来检索行。在本例中，由于计数大于一，所以返回翻译字符串的复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您还可以在复数化字符串中定义占位符属性。这些占位符可以通过将数组作为 <code v-pre>trans_choice</code> 函数的第三个参数传递替换：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'minutes_ago'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{1} :value minute ago|[2,*] :value minutes ago'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'time.minutes_ago'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想显示传递给 <code v-pre>trans_choice</code> 函数的整数值，可以使用内置的 <code v-pre>:count</code> 占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|{1} There is one|[2,*] There are :count'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="重写扩展包的语言文件" tabindex="-1"><a class="header-anchor" href="#重写扩展包的语言文件"><span>重写扩展包的语言文件</span></a></h2>
<p>某些扩展包可能会附带它们自己的语言文件。您可以在 <code v-pre>lang/vendor/{package}/{locale}</code> 目录中放置文件来覆盖重写这些文件，而不是更改包的核心文件来调整这些内容。</p>
<p>例如，如果您需要重写名为 <code v-pre>skyrim/hearthfire</code> 的包中 <code v-pre>messages.php</code> 的英语翻译字符串，您应该将语言文件放置在 <code v-pre>lang/vendor/hearthfire/en/messages.php</code>。在这个文件中，您只需定义您希望重写的翻译字符串。任何您没有重写的翻译字符串仍将从包的原始语言文件中加载。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/localizationmd/16681" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/lo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/localizationmd/16681" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/lo...</a></p>
</blockquote>
</div></template>


